from peewee import *
from search_public_srv.config import config
from time import time


class BaseModel(Model):
    create_time = BigIntegerField(default=time() * 1000, null=False, verbose_name="创建时间")
    update_time = BigIntegerField(default=0, null=False, verbose_name="更新时间")
    del_time = IntegerField(default=0, null=False, verbose_name="删除时间")

    def save(self, *args, **kwargs):
        self.update_time = time() * 1000
        return super(BaseModel, self).save(*args, **kwargs)

    def soft_delete(self):
        self.del_time = time()
        return super(BaseModel, self).save()

    @classmethod
    def select(cls, *selection):
        # 在大多数情况下,此方法有效,但在某些情况下,如果使用带有关键字“ IN”的联接结束结果查询时出现错误,则选择它会中断：“ 1241,’操作数应包含1列”
        return super(BaseModel, cls).select(cls, *selection).where(cls.del_time == 0)

    class Meta:
        database = config.DB


STATUS_CHOICES = (
    (0, "禁用"),
    (1, "启用")
)


class Banner(BaseModel):
    title = CharField(null=False, verbose_name="标题")
    img_url = CharField(null=False, verbose_name="图标地址")
    status = IntegerField(null=False, default=1, verbose_name="是否启用")
    sort = IntegerField(null=False, default=50, verbose_name="排序 从小到大")
    url = CharField(null=False, default="", verbose_name="前端跳转页面URL")
    about = CharField(null=False, default="", verbose_name="备注")


class Notice(BaseModel):
    title = CharField(null=False, verbose_name="标题")
    status = IntegerField(null=False, default=1, verbose_name="是否启用")
    sort = IntegerField(null=False, default=50, verbose_name="排序 从小到大")
    content = TextField(null=False, verbose_name="内容")


if __name__ == '__main__':
    models = [Banner, Notice]
    config.DB.create_tables(models)
    print("创建成功")
